/**
 * 
 */
package kr.co.insoft.publicmesh.controller.configration;

import kr.co.insoft.core.model.configration.sev.SEVEntityModel;
import kr.co.insoft.core.model.paging.PagingModel;
import kr.co.insoft.core.service.configration.cdt.CDTService;
import kr.co.insoft.core.service.configration.sev.SEVService;
import kr.co.insoft.framework.exceptions.ServiceException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @author IN-SOFT Inc. GoodwillDD(sylee@in-soft.co.kr)
 * 
 */
@Controller
@RequestMapping(value = "configration/sev")
public class SEVController {

	final String TILES_PAGE_PREFIX = "tiles/configration/sev";
	final String SINGLE_PAGE_PREFIX = "configration/sev";

	@Autowired
	SEVService sevService;

	@Autowired
	CDTService cdtService;

	/**
	 * Variable 목록
	 * 
	 * @param model
	 * @param pagingModel
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "lists")
	public String lists(
			Model model,
			@ModelAttribute("pagingModel") PagingModel pagingModel,
			@RequestParam(value = "ip", defaultValue = "N", required = false) String ip)
			throws Exception {
		model.addAttribute("pagingModel", sevService.getLists(pagingModel));

		if (ip.equals("Y"))
			return SINGLE_PAGE_PREFIX + "/lists";

		return TILES_PAGE_PREFIX + "/lists";
	}

	/**
	 * Variable 등록화면
	 * 
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "createForm", method = RequestMethod.GET)
	public String createForm(
			Model model,
			@RequestParam(value = "ip", defaultValue = "N", required = false) String ip) {
		model.addAttribute("sEVEntityModel", new SEVEntityModel());
		if (ip.equals("Y"))
			return SINGLE_PAGE_PREFIX + "/createForm";

		return TILES_PAGE_PREFIX + "/createForm";
	}

	@RequestMapping(value = "view", method = RequestMethod.GET)
	public String view(
			Model model,
			@RequestParam("variableid") String variableid,
			@RequestParam(value = "ip", defaultValue = "N", required = false) String ip)
			throws Exception {
		model.addAttribute("sEVEntityModel", sevService.getSEV(variableid));
		model.addAttribute("cdts", cdtService.getCDTs());
		if (ip.equals("Y"))
			return SINGLE_PAGE_PREFIX + "/view";

		return TILES_PAGE_PREFIX + "/view";
	}

	@RequestMapping(value = "simpleView", method = RequestMethod.GET)
	public String simpleView(Model model,
			@RequestParam("variableid") String variableid) throws Exception {
		model.addAttribute("sEVEntityModel", sevService.getSEV(variableid));
		model.addAttribute("cdts", cdtService.getCDTs());
		return SINGLE_PAGE_PREFIX + "/simpleView";
	}

	/**
	 * Variable 등록
	 * 
	 * @param sevEntityModel
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "createForm", method = RequestMethod.POST)
	public @ResponseBody ResponseEntity<Boolean> createVariable(
			@RequestParam("sevEntityData") String sevEntityData)
			throws Exception {
		try {
			sevService.createSEV(sevEntityData);
			return new ResponseEntity<Boolean>(true, HttpStatus.OK);
		} catch (ServiceException e) {
			return new ResponseEntity<Boolean>(false, HttpStatus.OK);
		}
	}

	@RequestMapping(value = "edit", method = RequestMethod.POST)
	public @ResponseBody ResponseEntity<Boolean> edit(
			@RequestParam("sevEntityData") String sevEntityData) {
		try {
			sevService.editSEV(sevEntityData);
			return new ResponseEntity<Boolean>(true, HttpStatus.OK);
		} catch (ServiceException e) {
			return new ResponseEntity<Boolean>(false, HttpStatus.OK);
		}
	}

	@RequestMapping(value = "remove", method = RequestMethod.POST)
	public @ResponseBody ResponseEntity<Boolean> remove(
			@RequestParam("sevEntityData") String sevEntityData) {
		try {
			sevService.removeSEV(sevEntityData);
			return new ResponseEntity<Boolean>(true, HttpStatus.OK);
		} catch (ServiceException e) {
			return new ResponseEntity<Boolean>(false, HttpStatus.OK);
		}
	}
}
